﻿

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;

namespace PiControls.Controls.MaskLayer
{

    /// <summary>
    /// 遮罩层
    /// </summary>
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]

    public  class PMaskLayer : Control
    {
        public PMaskLayer()
        {

            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.Opaque, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            CreateControl();
            Visible = false;
            this.BackColor = Color.Black;
            this.L_Alpha = 125;
            this.L_EnabledClickHide = true;
            this.TabIndex = 3;
        }



        private int _Alpha = 125;
        /// <summary>
        /// 透明度<para>范围：0~255（完全透明~完全不透明）</para><para>默认：125（半透明）</para>
        /// </summary>
        [Category("DemoUI"), Description("透明度\r\n范围：0~255（完全透明~完全不透明）\r\n默认：125（半透明）")]
        public int L_Alpha
        {
            get { return _Alpha; }
            set
            {
                if (value < 0) value = 0;
                if (value > 255) value = 255;
                _Alpha = value;
                Invalidate();
            }
        }

        /// <summary>
        /// 是否处于显示状态
        /// </summary>
        [Category("LESLIE_UI"), Description("是否处于显示状态"), Browsable(false)]
        public bool L_IsShow { get; private set; } = true;

        /// <summary>
        /// 是否启用点击隐藏功能<para>默认：是</para>
        /// </summary>
        [Category("DemoUI"), Description("是否启用点击隐藏功能\r\n默认：是")]
        public bool L_EnabledClickHide { get; set; } = true;

        /// <summary>
        /// OnPaint
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            SolidBrush BackColorBrush = new SolidBrush(Color.FromArgb(_Alpha, BackColor));
            e.Graphics.FillRectangle(BackColorBrush, e.ClipRectangle);
            BackColorBrush.Dispose();
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            if (L_EnabledClickHide)
            {
                L_HideMask();
            }
        }

        /// <summary>
        /// 显示遮罩层
        /// </summary>
        public void L_ShowMask()
        {
            try
            {
                this.BeginInvoke(new Action(() =>
                {
                    L_IsShow = true;
                    //   SendKeys.Send("{Tab}");
                    Dock = DockStyle.Fill;
                    BringToFront();
                    Visible = true;
                    Show();
                }));
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// 隐藏遮罩层
        /// </summary>
        public void L_HideMask()
        {
            try
            {
                this.BeginInvoke(new Action(() =>
                {
                    L_IsShow = false;
                    SendToBack();
                    Visible = false;
                    Hide();
                }));
            }
            catch (Exception)
            {
            }
        }


    }



}
